*
* Purpose: Prepare student-level data
*

clear all
use "${data_applications}", clear

sort id rank

bys id: egen funding_min = min(funding)
bys id: egen funding_max = max(funding)

gen funded_only = funding_max == 1
gen unfunded_only = funding_min == 2
drop funding_min funding_max

bys id: gen contract_number = _N
bys id program_id: gen counter_temp = _n
replace counter_temp = . if counter_temp > 1
bys id: egen program_number = sum(counter_temp)
drop counter_temp

keep if rank_of_admission == rank | (rank_of_admission == . & rank == 1)

gen disadv = disadv1 == 1 | disadv2 == 1
label variable disadv "Disadvantaged"
drop disadv1 disadv2

gen grammarschool = schooltype == 1
label variable grammarschool "Secondary grammar school"

label variable ses "NABC-based SES index"	
label variable disadv "Disadvantaged"
label variable GPA11_3 "11th-grade GPA (1--5)"
label variable gender "Female"
label variable program_number "Number of programs in ROL (observed)"
	
egen GPA11_3_std = std(GPA11_3)
label variable GPA11_3_std "11th-grade GPA (standardized)"
label variable rollength "Number of alternatives in ROL"
label variable grossincome "Per-capita annual gross income (1000 USD, 2007 prices)"

label variable budapest "Capital"
label variable county_town "County capital"

label variable funded_only "State-funded contract exclusively"
label variable unfunded_only "Self-funded contract exclusively"
label variable contract_number "Number of contracts on ROL (observed)"
label variable program_number "Number of programs on ROL (observed)"

keep funded_only unfunded_only disadv grossincome ses budapest county_town town ///
	village GPA11_3 gender rollength program_number mkod2002

compress
save "${data_students}", replace
